//
//  HMyQrCodeViewController.m
//  NursingManages
//
//  Created by HuangZhen on 2017/6/2.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HMyQrCodeViewController.h"
#import "HQrCodeGenerator.h"
#import "HMyQrCodeViewCell.h"
#import "UserDefault.h"

@interface HMyQrCodeViewController ()

@end

@implementation HMyQrCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.view addSubview:self.myCollectionView];
    [self.view addSubview:self.myPageControl];
}


-(UICollectionView*) myCollectionView{
    if(!_myCollectionView){
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(ScreenWidth, ScreenHeight-NavBarHeight - 50);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, NavBarHeight , ScreenWidth, ScreenHeight - NavBarHeight - 50) collectionViewLayout:layout];
        _myCollectionView.pagingEnabled = YES;
        _myCollectionView.showsHorizontalScrollIndicator = NO;
        _myCollectionView.bounces = NO;
        _myCollectionView.dataSource = self;
        _myCollectionView.delegate = self;
        
        [_myCollectionView registerClass:[HMyQrCodeViewCell class] forCellWithReuseIdentifier:@"cellId"];
    }
    return _myCollectionView;
}

-(UIPageControl *)myPageControl{
    if(!_myPageControl){
        _myPageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, 200, 50)];
        _myPageControl.center = self.view.center;
        _myPageControl.top = ScreenHeight - 50;
        
        _myPageControl.numberOfPages = 2;
        _myPageControl.backgroundColor = [UIColor clearColor];
        
        _myPageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
        _myPageControl.pageIndicatorTintColor = RGB(212, 212, 212);
        
    }
    return _myPageControl;
}

#pragma mark CollectionView 的委托


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 1;
}

- (void)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout cellCenteredAtIndexPath:(NSIndexPath *)indexPath page:(int)page{
    self.myPageControl.currentPage = page; // 分页控制器当前显示的页数
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HMyQrCodeViewCell *cell = (HMyQrCodeViewCell *)[self.myCollectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    
    UIImageView *code = nil;
    
    if(indexPath.section == 0){
    
        code = [self GetBarCodeImage];
        
    }else if(indexPath.section == 1){
        
        code = [self GetQrCodeImage];
    }
    
    [cell.contentView addSubview: code];
    
    NSString *currName =  [NSString stringWithFormat:@"%@(%@)", UserDefault.getUserInfo.USER_NAME,UserDefault.getUserInfo.USER_CODE ];
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, code.bottom + 15, ScreenWidth, 20)];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.text = currName;
    
    [cell.contentView addSubview: nameLabel];
    
    return cell;
}



-(UIImageView*) GetQrCodeImage{

    NSString *jobnum = UserDefault.getUserInfo.USER_CODE;
    
    CGFloat n = 200;
    CGFloat top = 50 + 30;
    
    UIImage *qrcode = [[[HQrCodeGenerator alloc]initWihtContentQrcode: jobnum]createQrCode:n];
    UIImageView *img = [[UIImageView alloc]initWithImage:qrcode];
    img.frame = CGRectMake(0, 0, n, n);
    img.center = self.view.center;
    img.top = top;
    return img;
}

-(UIImageView*) GetBarCodeImage{
    
    NSString *jobnum = UserDefault.getUserInfo.USER_CODE;
    
    CGSize m = CGSizeMake(200, 80);
    CGFloat top = 60 + 30;
    
    UIImage *barcode = [[[HQrCodeGenerator alloc ]initWihtContentBarcode:jobnum] createBarcode:m red:0 green:0 blue:0];
    UIImageView *img = [[UIImageView alloc]initWithImage:barcode];
    img.frame = CGRectMake(0, 0, 200, 100);
    img.center = self.view.center;
    img.top = top;
    
    return img;
}


#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    self.myPageControl.currentPage = (scrollView.contentOffset.x / ScreenWidth);
}

@end
